home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 2191 < prev    next >
Encoding:
Text File  |  1996-08-05  |  2.2 KB  |  49 lines

  1. Path: news.ov.com!news
  2. From: glenn@ov.com (Fletcher.Glenn@ov.com)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Beginner Question : Filling Arrays from T
  5. Date: 19 Jan 1996 19:18:49 GMT
  6. Organization: OpenVision
  7. Message-ID: <4doqqp$9t8@spanky.pls.ov.com>
  8. References: <1f7cc$13361c.91@news.hampshire.edu>
  9. Reply-To: glenn@ov.com
  10. NNTP-Posting-Host: foghorn.pls.ov.com
  11.  
  12. In article 91@news.hampshire.edu, <sbfF94@hamp.hampshire.edu> writes:
  13. >Hi, we're writing a cgi program in c to produce a multiple-choice quiz on a web page.  The program currently works, but we need to convert the contents of the question and answer arrays from being defined within the code:
  14. >
  15. >char *question[] =
  16. >{"ANT","BAT","CAT","DOG","EAR","FUN","GOO","HIPPO","ID","JON"};
  17.  
  18. This form of initialization only works at compile time.  This is because
  19. all of the entries are known, and therefore the size of the array is knowable.
  20.  
  21. >
  22. >to being filled by an external text file with an indeterminate (and changable) number of entries.  We are trying to figure out how to use fread() to do this, but are totally confused as to how.  We need to know : a)the required format of the text file to make the entire file readable into the array  b) how to get the array "question" to fill itself with as many pointers/strings as are in the text file and c)use the number of entries in the text file to constrain rand().
  23. >        Also, how does sizeof() work with fread, and do we need to use it?
  24. >we are compiling on unix using gcc.
  25. >                                                                                                                                                                                            Thanks,
  26. >                                                                                                                                                                                                        Shana Furman
  27. >                                                                                                                                                                                                        Jon Seagull
  28. >
  29.  
  30. The simplest possible way to make a dynamic array is to first read in the
  31. strings (with fgets) using dynamic allocation to build a linked list.  Once the
  32. linked list is created (and you know the number of entries), you can
  33. allocate the array of pointers and initialize those pointers.  Use the following
  34. struct to build your linked list:
  35.  
  36. struct ll {
  37.     char *string_data;
  38.     struct ll *forward_pointer;
  39. };
  40.  
  41. You will need:
  42.  struct ll *head, *tail;
  43. to keep track of the head of the list and where to put the next entry.
  44.  
  45.             Fletcher.Glenn@ov.com
  46.  
  47.  
  48.  
  49.